package teehee.UI;

import java.util.ArrayList;
import teehee.Manager.HistoryManager;
import teehee.Action.SearchPageAction;
import teehee.DB.DB;
import teehee.DB.DBHistory;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Class containing methods for the Exercise History Page
 * @author Connie
 */
public class ExerciseHistory extends Activity {
	Button update, graph;
	TextView pr, name, desc;
	public static DB input;
	LinearLayout history;
	public static boolean fromHistory = false;
	public static String descpt = "";
	
	/**
	 * Sets up page (xml layout, buttons, etc)
	 */
	@SuppressWarnings("static-access")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.exercisehistory);
	    
	    
	    pr = (TextView) findViewById(R.id.viewPR);
	    pr.setVisibility(View.INVISIBLE);
	    name = (TextView) findViewById(R.id.history_exerciseName);
	    desc = (TextView) findViewById(R.id.history_exerciseDescription);
	    history = (LinearLayout) findViewById(R.id.history_linearlayout);
	 	
	    if (AddNewTabs.goHistoryPage) {
	    	this.input = AddNewTabs.entry;
	    	AddNewTabs.goHistoryPage = false;
	    } 
	    else if(SearchPage.fromSearchPage){
	    	this.input = SearchPage.entry;
	    	SearchPage.fromSearchPage = false;
	    	/*if(SearchPage.entry!=null && SearchPage.entry.getName().equals("gary_gillespie"))
			{
				LinearLayout rl = (LinearLayout)findViewById(R.id.history_emptyline);
				rl.setBackgroundResource(R.drawable.garyblue);
			}*/
	    }
	    else if(WodPage.fromWodPage)
	    {
	    	this.input = WodPage.entry;
	    	WodPage.fromWodPage = false;
	    }
	    // link update button to the WOD page
		update = (Button) findViewById(R.id.history_updateButton); 
		update.setOnClickListener(new OnClickListener()  {
			public void onClick(View v) {
				fromHistory = true;
	    		Intent wodPage = new Intent(ExerciseHistory.this, WodPage.class);
	    		startActivity(wodPage);
	    	}
	    });
		
		// set up graph button
		/*graph = (Button) findViewById(R.id.history_graphButton); 
		graph.setOnClickListener(new OnClickListener() {
	    	public void onClick(View v) {
	    		Intent graphPage = new Intent(ExerciseHistory.this, WeightGraph.class);
	    		startActivity(graphPage);
	    	}
	    });*/
		
		// display history for wod or exercise
		if( HistoryManager.getType( input ).equals("wod") )
			 displayWOD();
		 else
			 displayExercise();
	}
	
	/**
	 * displays history for a user-specified WOD
	 */
	public void displayWOD() {
		// display name
	    displayName( HistoryManager.getFormattedName( input ) );
	    
	    // get exercise list and display description
	    String exList = "\n";
	    ArrayList<String> list = HistoryManager.getExerciseList( input );
	    for( int i = 0; i < list.size(); i++) {
	    	exList = exList + SearchPageAction.convertBack(list.get(i)) + '\n';
	    }
	    String descp = HistoryManager.getDescription( input );
	    descpt = exList + '\n' + descp + '\n';
	    this.displayDescription(descpt);
	    
	    ArrayList<DBHistory> wodHistory =
	    		DBHistory.getHistoryByName( HistoryManager.getName( input ),
	    										HistoryManager.getType( input ));
	    LinearLayout[] wodHistLayouts = new LinearLayout[ wodHistory.size() ];
	    
	    // header
	    LinearLayout wodHeader = new LinearLayout(this);
	    
	    TextView dateHeader = new TextView(this);
        dateHeader.setText( "Date" );
        wodHeader.addView(dateHeader);
        dateHeader.setWidth( 95 );
        formatHeaderText(dateHeader);
        
        TextView timeHeader = new TextView(this);
        timeHeader.setText( "Time" );
        wodHeader.addView(timeHeader);
        timeHeader.setWidth( 80 );
        formatHeaderText( timeHeader );
        
        TextView repsHeader = new TextView(this);
        repsHeader.setText( "Reps" );
        wodHeader.addView(repsHeader);
        repsHeader.setWidth( 80 );
        formatHeaderText( repsHeader );
        
        TextView commHeader = new TextView(this);
        commHeader.setText( "Comments" );
        wodHeader.addView(commHeader);
        commHeader.setWidth( 200 );
        formatHeaderText( commHeader );
        
	    history.addView(wodHeader); 
	    
	    // display list of all history
	    for(int j = 0; j < wodHistory.size(); j++) {
	    	wodHistLayouts[j] = new LinearLayout(this);
	        DBHistory entry = wodHistory.get( j );
	        
	    	// date
	        TextView date = new TextView(this);
	        date.setText( HistoryManager.getDate( entry ) );
	        wodHistLayouts[j].addView(date);
	        formatEntryText( date );
	        date.setWidth( 95 );
	        
	        // time
	        TextView time = new TextView(this);
	        time.setText( HistoryManager.getTime( entry ) );
	        wodHistLayouts[j].addView(time);
	        formatEntryText( time );
	        time.setWidth( 80 );
	        
	        // reps
	        TextView reps = new TextView(this);
	        reps.setText( HistoryManager.getReps( entry ) );
	        wodHistLayouts[j].addView(reps);
	        formatEntryText( reps );
	        reps.setWidth( 80 );
	        
	        // comments
	        TextView comments = new TextView(this);
	        comments.setText( HistoryManager.getComments( entry ) );
	        wodHistLayouts[j].addView(comments);
	        formatCommentText( comments ); 
	        
	        history.addView( wodHistLayouts[j] );
	    }	
	}
	
	/**
	 * displays history for a user-specified exercise
	 */
	public void displayExercise(){
		// hide update and graph buttons
		update.setVisibility(View.GONE);
		//graph.setVisibility(View.GONE);
		
		// display name and description
	    displayName( HistoryManager.getFormattedName( input ) );
	    descpt = HistoryManager.getDescription( input );
	    displayDescription( descpt );
	    
	    ArrayList<DBHistory> exHistory =
	    		DBHistory.getHistoryByName( HistoryManager.getName( input ),
	    										HistoryManager.getType( input ));
	    LinearLayout[] exHistLayouts = new LinearLayout[ exHistory.size() ];
	    
	    // header
	    LinearLayout exHeader = new LinearLayout(this);
	    
	    TextView dateHeader = new TextView(this);
        dateHeader.setText( "Date" );
        exHeader.addView(dateHeader);
        formatHeaderText( dateHeader );
        dateHeader.setWidth( 95 );
        
        TextView commHeader = new TextView(this);
        commHeader.setText( "Completed as Part of" );
        exHeader.addView(commHeader);
        commHeader.setWidth( 220 );
        formatHeaderText( commHeader );
	    
        history.addView(exHeader);
        
        // display list of all history
	    for(int j = 0; j < exHistory.size(); j++) {
	    	exHistLayouts[j] = new LinearLayout(this);
	    	DBHistory entry = exHistory.get(j);
	    	
	    	// date
	        TextView date = new TextView(this);
	        date.setText( HistoryManager.getDate( entry ) );
	        exHistLayouts[j].addView(date);
	        formatEntryText( date );
	        date.setWidth( 100 );
	        
	        // comments
	        TextView comments = new TextView(this);
	        comments.setText( HistoryManager.getComments( entry ) );
	        exHistLayouts[j].addView(comments);
	        formatEntryText( comments );
	        comments.setWidth( 200 );
	        
	    	history.addView(exHistLayouts[j]);
	    }   
	}
	
	////////// TEXTVIEW DISPLAY METHODS //////////
	
	/**
	 * Displays the WOD/exercise name using a TextView object.
	 * @param n the name to be displayed
	 */
	public void displayName( String n ) {
		name.setText( SearchPageAction.convertBack(n) );
	}
	
	/**
	 * Displays the WOD/exercise Personal Record using a TextView object.
	 * @param n the PR to be displayed
	 */
	public void displayPR( String p ) {
		pr.setText( SearchPageAction.convertBack(p) );
	}
	
	/**
	 * Displays the WOD/exercise description using a TextView object.
	 * @param n the description to be displayed
	 */
	public void displayDescription( String d ) {
		desc.setText( SearchPageAction.convertBack(d) );
	}
	
	////////// FORMATTING TEXTVIEW METHODS //////////
	
	/**
	 * Formats the text of the header titles (date, time, reps, comments)
	 * @param tv the TextView to be modified
	 */
	private void formatHeaderText( TextView tv ) {
		tv.setTextColor(Color.parseColor("#093055"));
        tv.setGravity( Gravity.CENTER );
        tv.setTypeface(null, Typeface.BOLD);
	}
	
	/**
	 * Formats the text of the date, reps, and time
	 * @param tv the TextView to be modified
	 */
	private void formatEntryText( TextView tv ){
        tv.setTextColor(Color.parseColor("#1D5285"));
        tv.setGravity( Gravity.CENTER );
        tv.setTextSize( 12 );
	}
	
	/**
	 * Formats the text of the comment
	 * @param tv the TextView to be modified
	 */
	private void formatCommentText( TextView tv ) {
		tv.setTextColor(Color.parseColor("#1D5285"));
        tv.setGravity( Gravity.CENTER );
        tv.setTextSize( 10 );
        tv.setWidth( 200 );
	}
	
	 @Override 
	    public boolean onKeyDown(int keyCode, KeyEvent event) 
	    {
	        if ((keyCode == KeyEvent.KEYCODE_MENU))
	        { 
	        	//setContentView(R.layout.main);
	        	Intent homep = new Intent(ExerciseHistory.this, MainPage.class);
	    		startActivity(homep);
	            return true;
	        }
	        
	        if ((keyCode == KeyEvent.KEYCODE_SEARCH))
	        { 
	        	//setContentView(R.layout.main);
	        	Intent homep = new Intent(ExerciseHistory.this, SearchPage.class);
	    		startActivity(homep);
	            return true;
	        }
	        
	        return super.onKeyDown(keyCode, event);    
	    } 
}
